import java.awt.BorderLayout;
import java.awt.Dimension;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.jbox2d.callbacks.DebugDraw;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Color3f;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;


public class Main {

	public static void main(String[] args) throws InterruptedException, InvocationTargetException {
		final JFrame frame = new JFrame("Box2D Test");
		JPanel screen = new JPanel();
		
		frame.add(screen, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		screen.setPreferredSize(new Dimension(900, 700));
		
		SwingUtilities.invokeAndWait(new Runnable() {
			@Override
			public void run() {
				frame.pack();
				frame.setVisible(true);
			}
		});
		
		DebugDraw2D draw = new DebugDraw2D(screen);
		World world = new World(new Vec2(0, -10), true);
		//GeometricDraw draw = new GeometricDraw(screen);
		
		world.setDebugDraw(draw);
		draw.setFlags(DebugDraw.e_shapeBit);

		BodyDef ground = new BodyDef();
		ground.position.set(0, -10);
		Body groundBody = world.createBody(ground);
		PolygonShape groundBox = new PolygonShape();
		groundBox.setAsBox(50, 10);
		groundBody.createFixture(groundBox, 0);

		// Make another Body that is dynamic, and will be subject to forces.
		//
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DYNAMIC; // dynamic means it is subject to forces
		bodyDef.position.set(2.0f, 4.0f);
		Body body = world.createBody(bodyDef);
		PolygonShape dynamicBox = new PolygonShape();
		dynamicBox.setAsBox(1.0f, 1.0f);
		FixtureDef fixtureDef = new FixtureDef(); // fixture def that we load up with the following info:
		fixtureDef.shape = dynamicBox; // ... its shape is the dynamic box (2x2 rectangle)
		fixtureDef.density = 1.0f; // ... its density is 1 (default is zero)
		fixtureDef.friction = 0.3f; // ... its surface has some friction coefficient
		body.createFixture(fixtureDef); // bind the dense, friction-laden fixture to the body
		
		// Simulate the world
		//
		float timeStep = 1.0f / 60.f;
		int velocityIterations = 10;
		int positionIterations = 8;
		for (int i = 0; i < 600; ++i) {
			//screen.repaint();
			world.drawDebugData();
			
			Thread.sleep(50);
			
			world.step(timeStep, velocityIterations, positionIterations);
			
			Vec2 position = body.getPosition();
			System.out.printf("%4.2f %4.2f\n", position.x, position.y);
			
			/*Vec2[] vertices = dynamicBox.getVertices();
			
			for(int j = 0; j < dynamicBox.getVertexCount(); j++) {
				//vertices[j] = vertices[j].add(position);
				vertices[j] = body.getWorldPoint(vertices[j]);
				//System.out.println("-- " + vertices[j]);
			}*/
			
			//screen.getGraphics().clearRect(0, 0, 900, 700);
			
			//draw.polygon(vertices, dynamicBox.getVertexCount(), Color3f.BLUE, Color3f.RED);
			//draw.polygon(groundBox.getVertices(), 4, Color3f.GREEN, Color3f.GREEN);
		}

		System.out.println("Done.");
	}

}
